//
//  YHYCaseShow.m
//  Home
//
//  Created by 杨泓晔 on 16/4/22.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYCaseShow.h"
#import "YHYDetailViewController.h"
#import <SDCycleScrollView.h>
#import <MWPhotoBrowser.h>


@interface YHYCaseShow ()<SDCycleScrollViewDelegate, MWPhotoBrowserDelegate>
{
    NSMutableArray *_photos;
    NSMutableArray *_thumbs;
    NSArray *array;
}


@property (nonatomic,retain)UIButton *button;
@property (nonatomic,retain)UIImageView *myImageView;
@property (nonatomic,retain)UILabel *nameL;
@property (nonatomic,retain)UILabel *priceL;
@property (nonatomic,retain)UILabel *sizeL;
@property (nonatomic,retain)UILabel *styleL;
@property (nonatomic,retain)UILabel *addressL;
@property (nonatomic,retain)SDCycleScrollView *imgView;
@end

@implementation YHYCaseShow
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView{
    //下一步
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 5;
    [self.contentView addSubview:self.button];
    [self.button setTitle:@"案例详情" forState:UIControlStateNormal];
  //  [self.button addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // [self.button setTitleColor:HS forState:UIControlStateNormal];
//    self.button.frame = CGRectMake(50.00*AUTO_WIDTH, 600.00*AUTO_WIDTH, 275.00*AUTO_WIDTH, 40.00*AUTO_WIDTH);
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(7 * AUTO_WIDTH);
        make.right.mas_equalTo(-7 * AUTO_WIDTH);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        
    }];
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:TEXTCONTENTFONT2 * TEXTWEIGHT];
    

    
    
    _myImageView = [UIImageView new];
    _myImageView.userInteractionEnabled=YES;
    [self.contentView addSubview:_myImageView];
    [_myImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.button.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.contentView.mas_left).offset(7 * AUTO_WIDTH);
        make.right.mas_equalTo(self.contentView.mas_right).offset(- 7 * AUTO_WIDTH);
        make.height.mas_equalTo(210 * AUTO_HEIGHT);
        
    }];
    
    array=@[@"u=957005295,3708602591&fm=206&gp=0.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg",@"u=957005295,3708602591&fm=206&gp=0.jpg"];
    
    self.imgView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-32 *AUTO_WIDTH, 210 *AUTO_HEIGHT) imageURLStringsGroup:array];
    self.imgView.clipsToBounds=YES;
    self.imgView.delegate=self;
    self.imgView.userInteractionEnabled=YES;
    
    self.imgView.infiniteLoop=YES;
    self.imgView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    self.imgView.autoScrollTimeInterval = 3;
    [_myImageView addSubview:self.imgView];

    
    
    
    _nameL = [UILabel new];
    _nameL.text = @"湖滨花园";
    _nameL.textColor = GH;
    _nameL.font = [UIFont fontWithName:TFont size:TEXTNAMEFONT * AUTO_WIDTH];
    [self.contentView addSubview:_nameL];
    [_nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_myImageView.mas_bottom).offset(5 * AUTO_HEIGHT);
        make.centerXWithinMargins .mas_equalTo(self.contentView);
        make.height.mas_equalTo(70/2 * AUTO_HEIGHT);
        
    }];
    
    UIImageView *line = [UIImageView new];
    [self.contentView addSubview:line];
    line.image = [UIImage imageNamed:@"虚线"];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameL.mas_bottom).offset(5 * AUTO_HEIGHT);
        make.left.mas_equalTo((14  )* AUTO_WIDTH);
        make.right.mas_equalTo(- (14) * AUTO_WIDTH);
        make.height.mas_equalTo(1.5 * AUTO_HEIGHT);
        
    }];
    
    //钱
    UIImageView *priceImageView = [UIImageView new];
    priceImageView.image = [UIImage imageNamed:@"qiang2x"];
    [self.contentView addSubview:priceImageView];
    [priceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(36 * AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(15 * AUTO_WIDTH, 15 * AUTO_WIDTH));
        
    }];
    
    UIImageView *suxian1 = [UIImageView new];
    [self.contentView addSubview:suxian1];
    suxian1.image = [UIImage imageNamed:@"虚线竖"];
    [suxian1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(85 * AUTO_WIDTH);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(- 30 * AUTO_HEIGHT);
    }];
    
    _priceL = [UILabel new];
    _priceL.text = @"30";
    _priceL.textColor = ZI;
    _priceL.textAlignment = NSTextAlignmentCenter;
    _priceL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
    [self.contentView addSubview:_priceL];
    [_priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(priceImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.height.mas_equalTo(priceImageView);
        make.left.mas_equalTo(self.contentView);
        make.right.mas_equalTo(suxian1.mas_right);
        
        
    }];
    
    
    
    //平方
    UIImageView *PingFangImageView = [UIImageView new];
    PingFangImageView.image = [UIImage imageNamed:@"mi"];
    [self.contentView addSubview:PingFangImageView];
    [PingFangImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(suxian1.mas_right).offset(35 * AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(15 * AUTO_WIDTH, 15 * AUTO_WIDTH));
        
    }];
    
    
    
    
    
    
    
    UIImageView *suxian2 = [UIImageView new];
    [self.contentView addSubview:suxian2];
    suxian2.image = [UIImage imageNamed:@"虚线竖"];
    [suxian2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(85 *2* AUTO_WIDTH);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(- 30 * AUTO_HEIGHT);
    }];
    _sizeL = [UILabel new];
    _sizeL = [UILabel new];
    _sizeL.text = @"110";
    _sizeL.textColor = ZI;
    _sizeL.textAlignment = NSTextAlignmentCenter;
    _sizeL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
    [self.contentView addSubview:_sizeL];
    [_sizeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.height.mas_equalTo(priceImageView);
        make.left.mas_equalTo(suxian1);
        make.right.mas_equalTo(suxian2.mas_right);
        
        
    }];
    
    
    UIImageView *homeIV = [UIImageView new];
    [self.contentView addSubview:homeIV];
    homeIV.image = [UIImage imageNamed:@"fangwu"];
    [homeIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(suxian2.mas_right).offset(33 * AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(18 * AUTO_WIDTH, 18 * AUTO_WIDTH));
        
    }];
    
    
    
    UIImageView *suxian3 = [UIImageView new];
    [self.contentView addSubview:suxian3];
    suxian3.image = [UIImage imageNamed:@"虚线竖"];
    [suxian3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(85 * 3* AUTO_WIDTH);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(- 30 * AUTO_HEIGHT);
    }];
    
    
    
    _styleL = [UILabel new];
    _styleL = [UILabel new];
    _styleL.text = @"平层户型";
    _styleL.textColor = ZI;
    _styleL.textAlignment = NSTextAlignmentCenter;
    _styleL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
    [self.contentView addSubview:_styleL];
    [_styleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.height.mas_equalTo(priceImageView);
        make.left.mas_equalTo(suxian2);
        make.right.mas_equalTo(suxian3.mas_right);
        
        
    }];
    
    
    UIImageView *addressIV = [UIImageView new];
    [self.contentView addSubview:addressIV];
    addressIV.image = [UIImage imageNamed:@"fenge"];
    [addressIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(15 * AUTO_HEIGHT);
        make.left.mas_equalTo(suxian3.mas_right).offset(40 * AUTO_WIDTH);
       // make.centerYWithinMargins.mas_equalTo(_addressL);
        make.size.mas_equalTo(CGSizeMake(15 * AUTO_WIDTH, 15 * AUTO_WIDTH));
        
        
    }];
    
    _addressL = [UILabel new];
    _addressL = [UILabel new];
    _addressL = [UILabel new];
    _addressL.text = @"地中海";
    _addressL.textColor = ZI;
    _addressL.textAlignment = NSTextAlignmentCenter;
    _addressL.font = [UIFont fontWithName:TFont size:TEXTALITTLEFONT * AUTO_WIDTH];
    
    [self.contentView addSubview:_addressL];
    [_addressL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(PingFangImageView.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.height.mas_equalTo(priceImageView);
        make.left.mas_equalTo(suxian3);
        //make.right.mas_equalTo(suxian3.mas_right);
        make.right.mas_equalTo(self.contentView);
        
        
    }];
    
    
    
    UIImageView *lineBottom = [UIImageView new];
    [self.contentView addSubview:lineBottom];
    lineBottom.image = [UIImage imageNamed:@"虚线"];
    [lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(suxian1.mas_bottom).offset(12 * AUTO_HEIGHT);
        make.left.width.height.mas_equalTo(line);
        
    }];

}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"tap tag is %ld", index);
    // Browser
    _photos = [[NSMutableArray alloc] init];
    _thumbs = [[NSMutableArray alloc] init];
    for (int i=0; i<array.count; i++)
    {
        MWPhoto *photo = [MWPhoto photoWithImage:[UIImage imageNamed:array[i]]];
        [_photos addObject:photo];
    }
    
    //调用父View的里面的方法
    YHYDetailViewController *vc = (YHYDetailViewController *)self.superview.superview.nextResponder;
    NSLog(@"%@", self.superview.superview.nextResponder);
    // Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    vc.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    browser.displayActionButton = NO;//分享按钮,默认是
    browser.displayNavArrows = YES;//左右分页切换,默认否
    browser.displaySelectionButtons = NO;//是否显示选择按钮在图片上,默认否
    browser.alwaysShowControls = NO;//控制条件控件 是否显示,默认否
    browser.zoomPhotosToFill = YES;//是否全屏,默认是
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;//是否全屏
#endif
    browser.enableGrid = YES;//是否允许用网格查看所有图片,默认是
    browser.startOnGrid = NO;//是否第一张,默认否
    browser.enableSwipeToDismiss = YES;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:index];
    
    
    [vc.navigationController pushViewController:browser animated:NO];
}

#pragma mark - MWPhotoBrowserDelegate
-(id<MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index
{
    return _photos[index];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}


- (id )photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

-(void) Back:(UIButton *) btn
{
    YHYDetailViewController *vc = (YHYDetailViewController *)self.superview.superview.nextResponder;
    [vc.navigationController popViewControllerAnimated:YES];
}


@end
